package com.batsoft.trade.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/10/20 17:19
 */
@Slf4j
public final class JedisUtil {

    private TradeSerialize serialize;

    public JedisUtil(TradeSerialize serialize) {
        this.serialize = serialize;
    }

    public static JedisPool jedisPool;

    private static final String HOST = "127.0.0.1";
    private static final Integer PORT = 6378;
    private static final String PASSWORD = "9191991";

//    private static final String HOST = "dev.gomyb.com";
//    private static final Integer PORT = 6378;
//    private static final String PASSWORD = "9191991";


    private static final Integer DATABASE = 3;
    private static final Integer TIMEOUT = 1000;

    private static Integer MAX_IDLE = GenericObjectPoolConfig.DEFAULT_MAX_IDLE;
    private static Integer MIN_IDLE = GenericObjectPoolConfig.DEFAULT_MIN_IDLE;
    private static Integer MAX_TOTAL = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL;

    static {
        // 未来这里取配置文件
        MAX_IDLE = 1000;
        MIN_IDLE = 500;
        MAX_TOTAL = 5000;

        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxIdle(MAX_IDLE);
        config.setMinIdle(MIN_IDLE);
        config.setMaxTotal(MAX_TOTAL);
        jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT, PASSWORD, DATABASE);
    }


    public Jedis getJedis() {
        return jedisPool.getResource();
    }

    public String hGet(String key, String field) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hget(key, field);
        } catch (Exception e) {
            log.error(Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return null;
    }

    public void zRem(String key, String... members) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.zrem(key, members);
        } catch (Exception e) {
            log.error(Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
    }

    public void hDel(String key, String field) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.hdel(key, field);
        } catch (Exception e) {
            log.error(Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
    }

    public Set<String> zRevRange(String key, int startIdx, int endIdx) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.zrevrange(key, startIdx, endIdx);
        } catch (Exception e) {
            log.error(Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return null;
    }

    public Set<String> zRange(String key, int startIdx, int endIdx) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.zrange(key, startIdx, endIdx);
        } catch (Exception e) {
            log.error(Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return null;
    }
}
